﻿// version:1.0.1
var bootstrapWrapper = {
    ver: '1.0',
    modalInstArr: [],
    showDialog: function (id, title, url, callback, params) {
        // 寻找有没有空闲的，如果有就直接用
        var emptyModalInst = null;
        if (this.modalInstArr.length > 0) {
            for (var index = 0; index < this.modalInstArr.length; index++) {
                if (this.modalInstArr[index].isShow == false) {
                    emptyModalInst = this.modalInstArr[index];
                    break;
                }
            }
        }
        if (emptyModalInst != null) {
            var modalId = 'modalContainer' + emptyModalInst.id;
            var modalTitleId = 'modalTitle' + emptyModalInst.id;
            var modalFrameId = 'modalFrame' + emptyModalInst.id;
            var modalDlgId = 'modalDialog' + emptyModalInst.id;

            var modalObj = $('#' + modalId);
            modalObj.modal({ backdrop: 'static', keyboard: false });//'show');

            document.getElementById(modalFrameId).src = url;
            document.getElementById(modalTitleId).innerText = title;
            emptyModalInst.callback = callback;
            emptyModalInst.params = params;

            if (params) {
                if (params.width) {
                    emptyModalInst.modalDialogDomEl.style.width = (params.width + 60) + 'px';
                    emptyModalInst.frameDomEl.style.width = params.width + 'px';
                    emptyModalInst.autoResize = false;
                } else {
                    emptyModalInst.modalDialogDomEl.style.width = (500 + 60) + 'px';
                    emptyModalInst.frameDomEl.style.width = 500 + 'px';
                    emptyModalInst.autoResize = false;
                }
                if (params.height) {
                    emptyModalInst.modalDialogDomEl.style.height = (params.height + 50) + 'px';
                    emptyModalInst.frameDomEl.style.height = params.height + 'px';
                    emptyModalInst.autoResize = false;
                } else {
                    emptyModalInst.modalDialogDomEl.style.height = (400 + 50) + 'px';
                    emptyModalInst.frameDomEl.style.height = 400 + 'px';
                    emptyModalInst.autoResize = false;
                }
            }
            return emptyModalInst;
        } else {
            var modalId = 'modalContainer' + id;
            var modalTitleId = 'modalTitle' + id;
            var modalFrameId = 'modalFrame' + id;
            var modalDlgId = 'modalDialog' + id;
            var htmlTemplate = '<div id="' + modalId + '" class="modal"><div class="modal-dialog" id="' + modalDlgId + '"><div class="modal-content"  style="padding:5px;"><div class="modal-header" style="padding:5px;"><h4 class="blue bigger" id="' + modalTitleId + '">' + title + '</h4></div><div class="modal-body"><iframe id="' + modalFrameId + '" src="' + url + '" frameborder="no" border="0"></iframe></div></div></div></div>';
            var modalEl = document.createElement('div');
            modalEl.id = 'modalWrapper' + id;
            modalEl.innerHTML = htmlTemplate;
            document.body.appendChild(modalEl);

            var modalObj = $('#' + modalId);
            modalObj.modal({ backdrop: 'static', keyboard: false });//'show');

            var modalFrameEl = document.getElementById(modalFrameId);

            var modalInst = {
                id: id,
                modalDialogDomEl: document.getElementById(modalDlgId),
                frameDomEl: modalFrameEl,
                modal: modalObj,
                callback: callback,
                autoResize: true,
                isShow: true,
                params: params
            };

            if (params) {
                if (params.width) {
                    modalInst.modalDialogDomEl.style.width = (params.width + 60) + 'px';
                    modalInst.frameDomEl.style.width = params.width + 'px';
                    modalInst.autoResize = false;
                } else {
                    modalInst.modalDialogDomEl.style.width = (500 + 60) + 'px';
                    modalInst.frameDomEl.style.width = 500 + 'px';
                    modalInst.autoResize = false;
                }
                if (params.height) {
                    modalInst.modalDialogDomEl.style.height = (params.height + 50) + 'px';
                    modalInst.frameDomEl.style.height = params.height + 'px';
                    modalInst.autoResize = false;
                } else {
                    modalInst.modalDialogDomEl.style.height = (400 + 50) + 'px';
                    modalInst.frameDomEl.style.height = 400 + 'px';
                    modalInst.autoResize = false;
                }
            }
            this.modalInstArr.push(modalInst);
            return modalInst;
        }
    },
    closeDialog: function (modalInst, result, data) {

        var modalId = 'modalContainer' + modalInst.id;
        var modalFrameId = 'modalFrame' + modalInst.id;
        var wrapperId = 'modalWrapper' + modalInst.id;

        var modalObj = $('#' + modalId);
        modalObj.modal('hide');

        //var modalEl = document.getElementById(wrapperId);
        //modalEl.parentElement.removeChild(modalEl);

        modalInst.callback(result, data, modalInst.params);
        modalInst.isShow = false;
    },
    confirm: function (title, message, callback) {
        bootbox.confirm({
            buttons: {
                confirm: {
                    label: '确认',
                    className: 'btn btn-sm btn-info'
                },
                cancel: {
                    label: '取消',
                    className: 'btn btn-sm'
                }
            },
            message: message,
            callback: callback,
            title: title
        });
    },
    alert: function (title, message, callback) {

        // debugger;
        bootbox.alert({
            buttons: {
                ok: {
                    label: '确认',
                    className: 'btn btn-sm btn-info'
                }
            },
            message: message,
            callback: callback,
            title: title
        });
    },
    growl: function (message) {
        //$.bootstrapGrowl(message, { type: 'success' });

        $.gritter.add({
            title: '消息',
            text: message,
            class_name: 'gritter-info'
        });
    },
    growlInfo: function (message) {
        $.gritter.add({
            title: '消息',
            text: message,
            class_name: 'gritter-info'
        });
    },
    growlWarning: function (message) {
        $.gritter.add({
            title: '消息',
            text: message,
            class_name: 'gritter-warning'
        });
    },
    growlError: function (message) {
        //$.bootstrapGrowl(message, { type: 'danger' });

        $.gritter.add({
            title: '错误',
            text: message,
            class_name: 'gritter-error'
        });
    }
};

var cppei = {
    _isBusy: false,
    frame: null,
    modalInstArr: new Array(),
    run:function(main){
        if (this.install()) {
            main();
        }
    },
    install: function () {
        if (window.parent.cppei_frame) {
            cppei.frame = window.parent.cppei_frame;

            //cppei.modalInst = window.parent.cppei_frame.modalStack[window.parent.cppei_frame.modalStack.length - 1];
            return true;
        } else if (window.parent.parent.cppei_frame) {
            cppei.frame = window.parent.parent.cppei_frame;
            return true;
        } else {
            if (window.location.href.toLowerCase().indexOf("module") > 0) {
                window.location.href = '../../index.html#' + window.location.href;
            }
        }
        return false;
    },
    showDialog: function (title, url, callback, params) {
        if (this.frame) {
            this.frame.showDialog(title, url, callback, params);
        }
    },
    closeDialog: function (result, data) {
        if (this.frame) {
            this.frame.closeDialog(result, data);
        }
    },
    confirm: function (title, message, callback) {
        if (this.frame) {
            this.frame.confirm(title, message, callback);
        } else {
            bootstrapWrapper.confirm(title, message, callback);
        }
    },
    alert: function (title, message, callback) {
        if (this.frame) {
            this.frame.alert(title, message, callback);
        } else {
            bootstrapWrapper.alert(title, message, callback);
        }
    },
    growl: function (message) {
        if (this.frame) {
            this.frame.growl(message);
        } else {
            bootstrapWrapper.growl(message);
        }
    },
    growlInfo: function (message) {
        if (this.frame) {
            this.frame.growlInfo(message);
        } else {
            bootstrapWrapper.growlInfo(message);
        }
    },
    growlWarning: function (message) {
        if (this.frame) {
            this.frame.growlWarning(message);
        } else {
            bootstrapWrapper.growlWarning(message);
        }
    },
    growlError: function (message) {
        if (this.frame) {
            this.frame.growlError(message);
        } else {
            bootstrapWrapper.growlError(message);
        }
    },
    api: {
        getUrl: function (sno, act) {
            return '/webapi2.ashx?sys:mod=cms&sys:sno=' + sno + '&sys:act=' + act;
        },
        getUrlWithTimespan: function (sno, act) {
            return '/webapi2.ashx?sys:mod=cms&sys:sno=' + sno + '&sys:act=' + act + '&_t=' + new Date().getTime();
        }
    },
    switchView: function (url) {
        if (this.frame != null) {
            this.frame.switchSubView(url, window.location.href);
        } else {
            window.location.href = url;
        }
    },
    log: function (message) {
        if (console) {
            console.log(message);
        }
    },
    busy: function (isBusy, message) {
        if (this.frame != null) {
            this.frame.busy(isBusy, message);
        } else {
            this.busyPartial(isBusy, message);
        }
    },
    busyPartial: function (isBusy, message) {
        if (isBusy) {
            if (this._isBusy == false) {
                if (message) {
                    $.blockUI({ message: "<label style='border:1px solid gray; background:#FFF; padding:5px;'>" + message + "</label>", css: { border: '0px', background: 'none' } });
                } else {
                    $.blockUI({ message: "<label style='border:1px solid gray; background:#FFF; padding:5px;'>处理中，请稍候...</label>", css: { border: '0px', background: 'none' } });
                }
                this._isBusy = true;
            }
        } else {
            if (this._isBusy == true) {
                $.unblockUI();
                this._isBusy = false;
            }
        }
    }
};